<?php

if(!defined('IN_DISCUZ') || !defined('IN_DISCUZ')) {
	exit('Access Denied');
}
$operation = in_array($_GET['operation'],array('list','setting','test','overage')) ? $_GET['operation'] : 'setting';



if(!$_GET['inajax']){
	cpheader();
	$setting = C::t('common_setting')->fetch_all(null);
	if($setting['sms']) $setting = unserialize($setting['sms']);
	
	shownav('founder', 'menu_founder_sms');
	$showsubmenus = array(
		array('sms_setting', 'sms&operation=setting', $operation == 'setting'),
		array('sms_setting_test', 'sms&operation=test', $operation == 'test'),
		array('sms_list', 'sms&operation=list', $operation == 'list'),
	);
	
	showsubmenu('menu_founder_sms', $showsubmenus);
}




if($operation =='setting'){
	$sms_pages = array(1=>'register',2=>'login',3=>'passwordcp',4=>'getpassword',5=>'admincp',6=>'post', 7=>'admins');
	if(submitcheck('smssubmit')){
		$update = array();
		foreach($_POST['sms'] as $key => $value){
			if(in_array($key,array('numbers','sendtime','sendmax','sendnft'))){
				$update[$key] = intval($value);
			}elseif(in_array($key,array('api','gtype','content','signature','clienid','username','password'))){
				$update[$key] = trim($value);
			}
		}
		$update['apiservice'] = in_array($_POST['sms']['apiservice'],array('wd','cl','close')) ? $_POST['sms']['apiservice'] : 'close';
		foreach($_POST['sms']['sendpage'] as $key => $value){
			$key = intval($key);
			if($key && $sms_pages[$key] == $value){
				$update['sendpage'][$key] = $value;
			}
		}
		$update['sendtime'] = max(60,$update['sendtime']);
		$update['sendmax'] = min(50,$update['sendmax']);
		$update['sendnft'] = min($update['sendmax'],$update['sendnft']);
		$update['numbers'] = min(10,$update['numbers']);
		
		
		$badlist = trim($_POST['sms']['badlist']);
		$badlist = explode("\n",$badlist);
		foreach($badlist as $key => $value){
			$value = preg_replace("/[^0-9\*]/",'',$value);
			if($value && strlen($value) == 11){
				$update['badlist'][] = $value;
			}
		}
		
		if($update) {
			C::t('common_setting')->update_batch(array('sms'=>$update));
			updatecache('setting');
		}
		cpmsg('setting_update_succeed', 'action=sms&operation=setting', 'succeed');
		
	}else{
		showtips('sms_setting_tips');
		showformheader('sms&operation=setting', 'smsforum');
		showhiddenfields(array('page' => $page, 'pp' => $_GET['pp'] ? $_GET['pp'] : $_GET['perpage']));
		showtableheader();
		showtitle('sms_setting');
		showsetting('sms_apiservice', array('sms[apiservice]', array(
			array('close', cplang('close')),
			array('wd', cplang('sms_apiservice_wd')),
			array('cl', cplang('sms_apiservice_cl')),
		)), $setting['apiservice'], 'select','','','<p id="sms_overage_return"></p>');
		showsetting('sms_username', 'sms[username]', $setting['username'], 'text');
		showsetting('sms_password', 'sms[password]', $setting['password'], 'text');
		showsetting('sms_clienid', 'sms[clienid]', $setting['clienid'], 'text');
		
		showtitle('sms_setting2');
		
		showsetting('sms_numbers', 'sms[numbers]', $setting['numbers'] ? intval($setting['numbers']) : 6, 'text');
		$varname = array('sms[sendpage]');
		foreach($sms_pages as $key => $value) {
			$varname[1][] = array($key, cplang('sms_sendpage_'.$key), $sms_pages[$key]);
		}
		showsetting('sms_sendpage', $varname, $setting['sendpage'], 'omcheckbox');
		
		showsetting('sms_sendtime', 'sms[sendtime]', $setting['sendtime'] ? intval($setting['sendtime']) : 90, 'text');
		showsetting('sms_sendmax', 'sms[sendmax]', $setting['sendmax'] ? intval($setting['sendmax']) : 10, 'text');
		showsetting('sms_sendntf', 'sms[sendnft]', intval($setting['sendnft']), 'text');
		showsetting('sms_signature', 'sms[signature]', $setting['signature'], 'text');
		showsetting('sms_content', 'sms[content]', $setting['content'], 'textarea');
		showsetting('sms_badlist', 'sms[badlist]', $setting['badlist'] ? implode("\n",$setting['badlist']) : '', 'textarea');
		showsubmit('smssubmit');
		showtablefooter();
		showformfooter();
		echo '<script type="text/javascript">ajaxget(\''.ADMINSCRIPT.'?action=sms&operation=overage\', \'sms_overage_return\')</script>';
	}
	
}elseif($operation =='list'){
	showformheader("sms&operation=list");
	showtableheader('search');
	showtablerow('', array('width="50"', 'width="170"', 'width="30"','width="120"','width="30"','width=""'),
		array(
			cplang('sms_field_mobile'), '<input size="15" name="mobile" type="text" value="'.$_GET['mobile'].'" />',
			cplang('sms_field_status'), '<select name="status">
			<option value="all" '.($_GET['status'] =='all' ? ' selected="selected"' :'').'>'.cplang('sms_field_all').'</option>
			<option value="0" '.($_GET['status'] =='0' ? ' selected="selected"' :'').'>'.cplang('sms_field_status_0').'</option>
			<option value="1" '.($_GET['status'] =='1' ? ' selected="selected"' :'').'>'.cplang('sms_field_status_1').'</option>
			<option value="-1" '.($_GET['status'] =='-1' ? ' selected="selected"' :'').'>'.cplang('sms_field_status_2').'</option>
			</select>',
			cplang('sms_field_page'), '<select name="modules">
			<option value="0" '.($_GET['modules'] =='0' ? ' selected="selected"' :'').'>'.cplang('sms_field_all').'</option>
			<option value="1" '.($_GET['modules'] =='1' ? ' selected="selected"' :'').'>'.cplang('sms_sendpage_1').'</option>
			<option value="2" '.($_GET['modules'] =='2' ? ' selected="selected"' :'').'>'.cplang('sms_sendpage_2').'</option>
			<option value="3" '.($_GET['modules'] =='3' ? ' selected="selected"' :'').'>'.cplang('sms_sendpage_3').'</option>
			<option value="4" '.($_GET['modules'] =='4' ? ' selected="selected"' :'').'>'.cplang('sms_sendpage_4').'</option>
			<option value="5" '.($_GET['modules'] =='5' ? ' selected="selected"' :'').'>'.cplang('sms_sendpage_5').'</option>
			<option value="5" '.($_GET['modules'] =='6' ? ' selected="selected"' :'').'>'.cplang('sms_sendpage_6').'</option>
			</select>',
		)
	);
	showtablefooter();
	showsubmit('listsubmit', 'submit');
	showformfooter();
	showtableheader();
	$perpage = 20;
	$page = max(1,intval($_GET['page']));
	$uids = array();
	$sql = '';
	if($_GET['mobile']){
		$_GET['mobile'] = preg_replace("/[^0-9]/",'',$_GET['mobile']);
		if($_GET['mobile']){
			$sql .= " AND mobile='".$_GET['mobile']."'";
		}
	}
	if(in_array($_GET['status'],array('0','1','-1'))){
		$sql .= " AND status='".$_GET['status']."'";
	}
	if(in_array($_GET['modules'],array('1','2','3','4','5'))){
		$sql .= " AND modules ='".$_GET['modules']."'";
	}
	$start = ($page - 1) * $perpage;
	$count = C::t('common_smscode')->fetch_count($sql);
	$multipage = multi($count, $perpage, $page, ADMINSCRIPT.'?action=sms&operation=list&mobile='.$_GET['mobile'].'&status='.$_GET['status']);
	$query = C::t('common_smscode')->fetch($sql,$start,$perpage);
	foreach($query as $key => $value){
		$query[$key]['username'] = '';
		if($value['uid'] >0){
			$uids[$value['uid']] = $value['uid'];
		}
	}
	$members = C::t('common_member')->fetch_all_username_by_uid($uids);
	foreach($query as $key => $value){
		if($members[$value['uid']]) $query[$key]['username'] = $members[$value['uid']];
	}
	showtablerow('', array('class="td25"', 'class=""', 'class="td28"'), array(
		cplang('sms_field_sid'),
		cplang('sms_field_page'),
		cplang('sms_field_uid'),
		cplang('sms_field_mobile'),
		cplang('sms_field_code'),
		cplang('sms_field_status'),
		cplang('sms_field_dateline'),
		cplang('sms_field_callback'),
	));
	
	foreach($query as $key => $value){
		showtablerow('', array('class="td25"', 'class=""', 'class="td28"'), array(
			$value['sid'],
			cplang('sms_sendpage_'.$value['modules']),
			$value['username'] ? '<a href="home.php?mod=space&uid='.$value['uid'].'" target="_blank">'.$value['username'].'</a>' : '',
			$value['mobile'],
			$value['code'],
			cplang('sms_field_status_'.$value['status']),
			dgmdate($value['dateline']),
			$value['callback']
		));
	}
	showtablefooter();
	echo $multipage;
	
}elseif($operation =='test'){
	
	if($_GET['inajax']){
		$setting = $_G['setting']['sms'] && !is_array($_G['setting']['sms']) ? unserialize($_G['setting']['sms']) : $_G['setting']['sms'];
		$Status = 0;
		if($_GET['ajax_test_smsmobiles']){
			$SMS = new SMS();
			$SMS->CN = $cn;
			$SMS->CONF_send_debug = 1;
			$return = $SMS ->send(end(array_keys($setting['sendpage'])), $_GET['ajax_test_smsmobiles']);
			if($return['status'] == 1){
				$Status = 1;
			}
		}
		ajaxshowheader();
		if($Status ==1){
			echo '<div style="color:#3B8600; font-size:14px; padding-bottom:10px;"><strong>'.cplang('sms_api_test_succeed').'</strong></div>';
		}else{
			echo '<div style="color:#f00; font-size:14px; padding-bottom:10px;"><strong>'.cplang('sms_api_test_error').'</strong></div>';
		}
		if(!$SMS->error){
			if($return){
				if($return['data']){
					$return['data'] = '<div style="color:#888; padding:20px 0 0 60px;"><p style="color:#f00; padding-bottom:10px">'.cplang('sms_api_test_return').':</p>'.dhtmlspecialchars($return['data']).'</div>';
				}
				echo '<pre>';
				print_r($return);
				echo '</pre>';
			}
		}
		ajaxshowfooter();
		exit;
	}
	showtableheader();
	showtitle('sms_test_tips');
	showsetting('sms_field_mobile', 'smsmobiles', '', 'text','','','',' id="smsmobiles"');
	echo '<tr><td><input type="submit" class="btn" id="submit_listsubmit" name="listsubmit" value="'.cplang('submit').'" onclick="$(\'ajaxreturn\').innerHTML =\'\';ajaxget(\''.ADMINSCRIPT.'?action=sms&operation=test&ajax_test_smsmobiles=\'+$(\'smsmobiles\').value, \'ajaxreturn\')"></td></tr>';
	echo '<tr><td><div id="ajaxreturn"></div></td></tr>';
	showtablefooter();
	
}elseif($operation =='overage'){
	
	ajaxshowheader();
	list($overage,$gettime) = explode("\t",getcookie('smsoverage'));
	$overage = dintval($overage);
	$gettime = dintval($gettime);
	$overageerror = 0;
	if($gettime < TIMESTAMP - 3600){
		$SMS = new SMS();
		$overage = $SMS ->overage();
		if($SMS->error){
			$overageerror = $SMS->error;
		}elseif($overage >0){
			dsetcookie('smsoverage',$overage."\t".TIMESTAMP);
		}
	}
	echo '<span style="color:#369;"><strong>'.cplang('sms_overage').': ';
	if($overageerror){
		echo '<font style="color:#f00">'.lang('message','smscode_error_'.$overageerror).'</font>';
	}elseif($overage >0){
		
		echo '<font style="color:#f00">'.$overage.'</font> '.cplang('sms_overage_s');
	}else{
		echo '<font style="color:#f00">'.cplang('sms_overage_0').'</font>';
	}
	
	echo '</strong></span>';
	ajaxshowfooter();
}
?>